﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyForms;
using QLNH.QLNH_BUS_WS;
using System.Threading;

namespace QLNH.MyLayouts
{
    public partial class LayoutChef : LayoutUC
    {
        public LayoutChef(MainForm mainForm)
        {
            this.mainForm = mainForm;
            InitializeComponent();
        }

        private void getAllOrder()
        {
            Thread t = new Thread(new ThreadStart(getAllOrderThread));
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();
        }

        private void getAllOrderThread()
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            OrderDetailDTO[] orders = ws.GetAllOrderForChef();

            if (orders != null)
            {
                for (int i = 0; i < orders.Length; i++)
                {
                    ListViewItem li = new ListViewItem()
                    {
                        Name = orders[i].OrderNo.ToString(),
                        Text = orders[i].TableID,
                        ImageKey = orders[i].State.ToString()
                    };
                    li.SubItems.Add(orders[i].OrderNo.ToString());
                    li.SubItems.Add(orders[i].UserName);
                    li.SubItems.Add(orders[i].Meals.Count().ToString());

                    lv_requestList.BeginInvoke((MethodInvoker)(() => lv_requestList.Items.Add(li)));
                }
            }
        }

        private void getOrderDetail()
        {
            lv_requestDetail.Items.Clear();

            Thread t = new Thread(new ThreadStart(getOrderDetailThread));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        private void getOrderDetailThread()
        {
            Thread.Sleep(100);

            int orderNo = Int32.Parse(lv_requestList.SelectedItems[0].Name);

            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            OrderDetailDTO order = ws.GetOrder(orderNo);

            for (int i = 0; i < order.Meals.Count(); i++)
            {
                ListViewItem li = new ListViewItem()
                {
                    Text = order.Meals[i].MealName
                };
                li.SubItems.Add(order.Meals[i].Amount.ToString());

                lv_requestDetail.BeginInvoke((MethodInvoker)(() => lv_requestDetail.Items.Add(li)));
            }
        }

        public override void OnReceivePackage(IRemoting.BasePackage package)
        {
            if (package.getBroadcastMassageHeader() == IRemoting.BroadcastMassgeHeader.NEW_ORDER)
            {
                QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
                OrderDetailDTO order = new OrderDetailDTO();
                order = ws.GetOrder(Int32.Parse(package.getBroadcastMassage()));

                ListViewItem li = new ListViewItem()
                {
                    Name = order.OrderNo.ToString(),
                    Text = order.TableID,
                    ImageKey = order.State.ToString()
                };
                li.SubItems.Add(order.OrderNo.ToString());
                li.SubItems.Add(order.UserName);
                li.SubItems.Add(order.Meals.Count().ToString());

                lv_requestList.BeginInvoke((MethodInvoker)(() => lv_requestList.Items.Add(li)));
            }
            else if (package.getBroadcastMassageHeader() == IRemoting.BroadcastMassgeHeader.MODIFIED_ORDER)
            {
                QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
                OrderDetailDTO order = new OrderDetailDTO();
                order = ws.GetOrder(Int32.Parse(package.getBroadcastMassage().ToString()));

                ListViewItem li = lv_requestList.Items.Find(package.getBroadcastMassage(), false)[0];
                li.SubItems[0].Text = order.TableID;
                li.SubItems[1].Text = order.OrderNo.ToString();
                li.SubItems[2].Text = order.UserName;
                li.SubItems[3].Text = order.Meals.Count().ToString();
                li.ImageKey = order.State.ToString();
            }
        }

        private void LayoutChef_Load(object sender, EventArgs e)
        {
            getAllOrder();
        }

        private void lv_requestList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lv_requestList.SelectedItems.Count > 0)
            {
                bt_completeAll.Enabled = true;

                int orderNo = Int32.Parse(lv_requestList.SelectedItems[0].Name);

                getOrderDetail();
            }
            else
            {
                bt_completeAll.Enabled = false;
            }
        }

        private void bt_completeAll_Click(object sender, EventArgs e)
        {
            if (lv_requestList.SelectedItems.Count > 0)
            {
                int orderNo = Int32.Parse(lv_requestList.SelectedItems[0].Name);
                mainForm.readyToServer(orderNo);
                lv_requestList.Items.Remove(lv_requestList.Items.Find(orderNo.ToString(), false)[0]);
                lv_requestDetail.Items.Clear();
            }
        }
        
    }
}
